package com.msconfig.mvp.view.fragment;

import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.geo.parse.GnssSateInfoList;
import com.msconfig.device.GeoLocationData;
import com.msconfig.event.DeviceUpdateEvent;
import com.msconfig.mvp.view.activity.base.BaseMvpFragment;
import com.msconfig.mvp.view.activity.base.BasePresenter;
import com.msconfig.widget.DrawEarth;
import com.msconfig.widget.DrawSatellite;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

public class SatelliteMapFragment extends BaseMvpFragment implements View.OnClickListener {
    private ImageView ivCommonBack;
    private TextView tvCommonTitle;
    private RelativeLayout relativeLayout;
    private DrawSatellite drawSatellite;
    private int heigth;
    private int width;
    private GnssSateInfoList mGnssSateInfoList;



    public static SatelliteMapFragment newInstance() {
        Bundle args = new Bundle();
        SatelliteMapFragment fragment = new SatelliteMapFragment();
        fragment.setArguments(args);
        return fragment;
    }

    //更新卫星信息
    @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
    public void showReceiveDataSuccess(DeviceUpdateEvent.UpdateSatelliteResultStatus event) {
        mGnssSateInfoList= GeoLocationData.getInstance().getSateInfoList();
        //绘制卫星图
        drawSatellite(mGnssSateInfoList);
    }

    private void drawSatellite(GnssSateInfoList mGnssSateInfoList) {
        //设置数据
        drawSatellite=new DrawSatellite(getContext(),width,heigth,mGnssSateInfoList);
        drawSatellite.invalidate();
        relativeLayout.addView(drawSatellite);
    }



    @Override
    protected int getLayoutId() {
        return R.layout.layout_fragment_satellite_map;
    }

    @Override
    protected void initView(View view) {
        ivCommonBack=view.findViewById(R.id.iv_common_back);
        tvCommonTitle=view.findViewById(R.id.tv_common_title);
        relativeLayout=view.findViewById(R.id.rl_layout);
    }

    @Override
    protected void initListener(View view) {
        ivCommonBack.setOnClickListener(this);
    }

    @Override
    protected void initData() {
        tvCommonTitle.setText(R.string.title_map);
        DisplayMetrics dm = getResources().getDisplayMetrics();
        heigth = dm.heightPixels;
        width = dm.widthPixels;

        //画地球背景
        DrawEarth earthView = new DrawEarth(getContext(), width, heigth);
        earthView.invalidate();
        relativeLayout.addView(earthView);

    }

    @Override
    public void onClick(View v) {
        if(v==ivCommonBack){
            backTo();
        }

    }

    @Override
    protected void createPresenter(List<BasePresenter> mPresenters) {

    }

}
